<?php

namespace App\Http\Controllers\Resources;

use App\Mail\UserMessage;
use App\Models\Group;
use App\Models\Admin;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Auth;
use App\Http\Requests;
use App\Services\HelpService;
use App\Http\Requests\UserRequest;
use App\Services\Interfaces\RepoServiceInterface;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;

class AdminController extends Controller
{
    public function __construct(RepoServiceInterface $_repoService)
    {
        $this->_repoService = $_repoService;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $user = Auth::User();
        if (!$this->authorize($user)) {
            abort(403);
        }
        /**@var Collection $data*/
        $data = $this->_repoService->read();
        $page = request()->input('page', 1);
        $res_data = [
            'total' => $data->count(),
            'per_page' => 20,
            'current_page' => $page,
            'data' => array_values($data->forPage($page, 20)
                ->toArray()),
        ];

        if(!empty($res_data))
            return $res_data;
        else
            return [];

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(UserRequest $request)
    {
        $user = Auth::User();
        if (!$this->authorize('store', $user)) {
            abort(403);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $user = Auth::User();
        $res = $this->_repoService->find(['id'=>$id]);
        if($res){
            return $res;
        } else {
            return $this->_error('没有找到指定的账号');
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UserRequest $request, $id)
    {
        $user = Auth::User();
        if (!$this->authorize($user)) {
            abort(403);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
